//
//  Reusable.swift
//  globalcarbusiness
//
//  Created by dongzhenyu on 2017/8/19.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit

protocol Reusable {
    static var reusableIdentifier: String {get}
    static var nib: UINib? {get}
}

extension Reusable {
    
    static var reusableIdentifier: String  {
        return "\(self)"
    }
    static var nib: UINib? {
        return nil
    }
    
}

extension UITableView {
    func registerCell<T: UITableViewCell>(_ cell: T.Type) where T:Reusable {
        if let nib = T.nib {
            register(nib, forCellReuseIdentifier: T.reusableIdentifier)
        } else {
            register(cell, forCellReuseIdentifier: T.reusableIdentifier)
        }
    }
    
    func dequeueReusableCell<T: Reusable>(indexPath: IndexPath) -> T {
        
        return dequeueReusableCell(withIdentifier: T.reusableIdentifier, for: indexPath) as! T
    }
}
